package com.example.dongha;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;

import com.example.dongha.Indicator.ColorTransitionPagerTitleView;
import com.example.dongha.Indicator.CommonNavigator;
import com.example.dongha.Indicator.CommonNavigatorAdapter;
import com.example.dongha.Indicator.IPagerIndicator;
import com.example.dongha.Indicator.IPagerTitleView;
import com.example.dongha.Indicator.LinePagerIndicator;
import com.example.dongha.Indicator.MagicIndicator;
import com.example.dongha.Indicator.SimplePagerTitleView;
import com.example.dongha.Indicator.ViewPagerHelper;
import com.example.dongha.adapter.TabPagerAdapter;
import com.example.dongha.bar.AxisUtils;
import com.example.dongha.fragment.SignsCountFragment;
import com.example.dongha.fragment.TotalCountFragment;
import com.example.dongha.fragment.SleepCountFragment;
import com.example.dongha.fragment.SportCountFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String[] CHANNELS = new String[]{"综合", "运动", "睡眠", "体征"};

    private List<String> mSrcList = Arrays.asList(CHANNELS);
    private List<Fragment> mDataList;
//    private ExamplePagerAdapter mExamplePagerAdapter = new ExamplePagerAdapter(mDataList);

    private ViewPager mViewPager;
    private MagicIndicator magicIndicator;
    private TabPagerAdapter tabPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDataList = new ArrayList<>();
        initFragments();
        initMagicIndicator();
    }

    private void initFragments() {
//        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager = findViewById(R.id.view_pager);
        TotalCountFragment oneFragment = new TotalCountFragment();
        SportCountFragment twoFragment = new SportCountFragment();
        SleepCountFragment threeFragment = new SleepCountFragment();
        SignsCountFragment fourFragment = new SignsCountFragment();
        mDataList.add(oneFragment);
        mDataList.add(twoFragment);
        mDataList.add(threeFragment);
        mDataList.add(fourFragment);
        tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), mDataList);
        mViewPager.setAdapter(tabPagerAdapter);
    }

    private void initMagicIndicator() {
        magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator1);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mSrcList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(Color.parseColor("#63677a"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FECB00"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(Color.parseColor("#FECB00"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerPadding((int) AxisUtils.dpToPixel(15));
//        titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.simple_splitter));
        ViewPagerHelper.bind(magicIndicator, mViewPager);
//        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                magicIndicator.onPageSelected(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//                magicIndicator.onPageScrollStateChanged(state);
//            }
//        });
    }

}
